{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 远程访问服务搭建\n",
    "\n",
    "## 环境准备\n",
    "```bash\n",
    "sudo apt-get install update\n",
    "sudo apt-get install python3 python-pip\n",
    "sudo pip3 install jupyter\n",
    "```\n",
    "\n",
    "\n",
    "## 修改pip源\n",
    "\n",
    "Linux编辑 ~/.pip/pip.conf ，在最上方加入如下内容：\n",
    "```ini\n",
    "[global]\n",
    "index-url = https://pypi.tuna.tsinghua.edu.cn/simple\n",
    "[install]\n",
    "trusted-host = https://pypi.tuna.tsinghua.edu.cn\n",
    "```\n",
    "\n",
    "## 虚拟环境（可选）\n",
    "\n",
    "```bash\n",
    "sudo pip install -U virtualenv\n",
    "virtualenv venv -p python3\n",
    "source venv/bin/activate\n",
    "```\n",
    "\n",
    "## 创建登陆密码\n",
    "\n",
    "```python\n",
    "from notebook.auth import passwd\n",
    "print(passwd(\"jupyter\"))\n",
    "```\n",
    "Out[2]: 'sha1:*******************'\n",
    "\n",
    "\n",
    "## 创建ssl证书（https连接需要，http可跳过）\n",
    "\n",
    ">openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem\n",
    "\n",
    "## 编辑配置文件\n",
    "\n",
    "**jupyter notebook --generate-config**\n",
    "\n",
    "root用户加上--allow-root\n",
    "\n",
    "在当前用户根目录下生成.**.jupyter/jupyter_notebook_config.py**文件\n",
    "修改以下内容：\n",
    "```python\n",
    "c.NotebookApp.allow_remote_access = True  # jupyter 4.5版本以上需要配\n",
    "c.NotebookApp.allow_root = True     #允许root用户运行\n",
    "c.NotebookApp.ip = '*'   # 允许远程访问（备用参数0.0.0.0）\n",
    "c.NotebookApp.notebook_dir = r'/root/jupyter'  # 启动目录\n",
    "c.NotebookApp.open_browser = False    # 默认不启动浏览器\n",
    "c.NotebookApp.password = 'sha1:xxxxxxxxxx' # 上面生成的密钥\n",
    "c.NotebookApp.certfile = u'c:/jpyb/mycert.pem'  # 指定文件路径\n",
    "c.NotebookApp.keyfile = u'c:/jpyb/mykey.key'  # 指定文件路径\n",
    "c.IPKernelApp.pylab = 'inline'    # 所有matplotlib的图像都通过iline的方式显示\n",
    "c.NotebookApp.port = 8888     # 运行的端口\n",
    "\n",
    "```\n",
    "\n",
    " - 查看端口是否占用脚本\n",
    ">netstat -anp|grep 8888\n",
    "\n",
    "## 启动jupyter\n",
    "\n",
    "```bash\n",
    "nohup jupyter notebook &\n",
    "```\n",
    "注：可以写入 sh 脚本，然后设置开机自启\n",
    "\n",
    "## 设置阿里云安全组（非阿里云用户无需这一步）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在`控制台`-`云服务器ESC`-`实例` 选择自己的主机点击`本实例安全组`-`安全组列表`-`配置规则`-`入方向`-`手动添加`添加如下图所示配置\n",
    "注：其中端口号填写自己设置的端口\n",
    "![](https://img-blog.csdnimg.cn/2020073110064542.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dhbnpoZW5nXzk2,size_16,color_FFFFFF,t_70#pic_center)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## ✨更换Logo\n",
    "\n",
    "图片地址：\n",
    "\n",
    ">/usr/local/lib/python3.5/dist-packages/notebook/static/base/images/logo.png\n",
    "\n",
    "## 👍安装扩展插件\n",
    "\n",
    "[github地址](https://github.com/ipython-contrib/jupyter_contrib_nbextensions)\n",
    "\n",
    ">pip3 install jupyter_contrib_nbextensions\n",
    "\n",
    ">pip3 install -U six\n",
    "\n",
    ">jupyter contrib nbextension install --user"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 打开Markdown为ipynb\n",
    "\n",
    "## 安装\n",
    "\n",
    "直接安装jupytext即可\n",
    "\n",
    "```sh\n",
    "pip install jupytext\n",
    "```\n",
    "\n",
    "## 其他方案\n",
    "\n",
    "插件主页：https://github.com/aaren/notedown\n",
    "\n",
    "`notedown` 插件可以使 ipynb 和 .md  文件进行互相转换，还可以配置在浏览器界面直接编辑和预览 markdown文件，还能将 md文件中的python代码块渲染成ipynb中可编辑可执行的 cell单元，总之很强大。\n",
    "\n",
    "```\n",
    "pip install notedown\n",
    "```\n",
    "\n",
    "或者安装开发版\n",
    "\n",
    "```sh\n",
    "pip install https://github.com/aaren/notedown/tarball/master\n",
    "```\n",
    "\n",
    "修改配置文件，（配置文件位置见上文[#1.6]部分）\n",
    "\n",
    "```py\n",
    "c.NotebookApp.contents_manager_class = 'notedown.NotedownContentsManager'\n",
    "```\n",
    "\n",
    "### 用法\n",
    "\n",
    "#### md 转 ipynb\n",
    "\n",
    "```sh\n",
    "notedown input.md > output.ipynb\n",
    "```\n",
    "\n",
    "####  ipynb 转 md\n",
    "\n",
    "**清空所有输出内容**\n",
    "\n",
    "```sh\n",
    "notedown input.ipynb --to markdown --strip > output.md\n",
    "```\n",
    "\n",
    "**保留所有输出，渲染成原始的json**\n",
    "\n",
    "```sh\n",
    "notedown input.ipynb --to markdown > output_with_outputs.md\n",
    "```\n",
    "\n",
    "**保留所有输出，渲染成原div 元素（不好使）**\n",
    "\n",
    "```sh\n",
    "notedown input.ipynb --to markdown  --render > output_with_outputs.md \n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 📢内核管理\n",
    "\n",
    "## 常用操作\n",
    "\n",
    "### 查看已安装内核的信息\n",
    "\n",
    "```sh\n",
    "jupyter kernelspec list\n",
    "```\n",
    "\n",
    "### 卸载\n",
    "\n",
    "卸载指定名称的内核，比如： java\n",
    "\n",
    "```sh\n",
    "jupyter kernelspec remove java\n",
    "```\n",
    "\n",
    "### 删除\n",
    "\n",
    "```sh\n",
    "jupyter kernelspec uninstall java   #java  \n",
    "```\n",
    "\n",
    "## 安装多版本python内核\n",
    "\n",
    "安装多个py内核后就能像虚拟环境一样运行不同版本的代码了，在jupyter 新建文件时可以选择不同的内核版本，也可以在上方菜单栏点击<kbd>Kernel</kbd> - > <kbd>Change kernel</kbd> 切换当前内核。\n",
    "\n",
    "当前场景：以在python3 中安装了jupyter，需要安装 python2 的内核\n",
    "\n",
    "1. 在python2（虚拟环境也可）下安装 ipykernel \n",
    "```sh\n",
    "python2 -m pip install ipykernel\n",
    "```\n",
    "\n",
    "2. 安装内核到jupyter 中\n",
    "```sh\n",
    "python2 -m ipykernel install --user --name py2 \n",
    "# --user安装到当前用户，可查看帮助选择不同安装位置，也可不加该参数，安装到默认位置。--name为内核命名为py2\n",
    "```\n",
    "3. 查看\n",
    "```sh\n",
    "jupyter kernelspec list\n",
    "```\n",
    "\n",
    "4. 重启jupyter\n",
    "\n",
    "## 🚀支持C++内核( 需要minicanda或者新建虚拟环境）\n",
    "\n",
    "\n",
    "### 利用windows子系统（WSL）或者Linux中\n",
    "\n",
    "（利用Win10子系统可视化开发环境搭建可参考鄙人文章）\n",
    "\n",
    "1. 从windows应用商店里安装Ubuntu子系统\n",
    "\n",
    "2. 安装 Miniconda\n",
    "\n",
    "这里使用 xeus-cling，安装说明指出需要 Miniconda，因为 Anaconda 会有冲突，因此我使用 Miniconda，已经装过 Anaconda 的可以尝试一下用 Anaconda 安装。\n",
    "\n",
    "在这里找到需要的 Miniconda 版本，然后复制链接：\n",
    "https://docs.conda.io/en/latest/miniconda.html\n",
    "\n",
    "通过命令行或者开始菜单打开上一步中安装的 Ubuntu，输入下列命令。\n",
    "```sh\n",
    "wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh\n",
    "bash Miniconda3-latest-Linux-x86_64.sh\n",
    "```\n",
    "出现 License 之后按 q，输入 yes 允许许可，然后 Enter，如果要改路径就自行修改。然后输入 yes 完成初始化\n",
    "\n",
    "输入`which python` 发现python路径与Windows里的python路径是隔离的，并不冲突。终于发现子系统的好处了😂。\n",
    "\n",
    "<https://github.com/QuantStack/xeus-cling>\n",
    "\n",
    "安装：\n",
    "\n",
    ">conda install xeus-cling -c conda-forge\n",
    "\n",
    "安装完之后再次打开 Jupyter notebook，可以在 New 按钮下看到多了 C++11，C++14 和 C++17，新建一个 C++14 notebook，输入一些 C++ 代码，Shift + Enter 可以得到运行结果，没有报错就大功告成了！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ☕支持Java\n",
    "\n",
    "![](assets/display-img.png)\n",
    "\n",
    "### 环境准备：\n",
    "\n",
    "1. Java JDK >= 9，注意不是jre\n",
    "\n",
    " i. 检查java环境\n",
    "```\n",
    "> java -version\n",
    "java version \"9\"\n",
    "Java(TM) SE Runtime Environment (build 9+181)\n",
    "Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)\n",
    "```\n",
    "\n",
    "ii. 接下来，确保Java位于jdk的安装位置，而不仅仅是jre, 使用`java --list-modules` .输出列表应当包含`jdk.jshell`.\n",
    " \n",
    "  - On *nix `java --list-modules | grep \"jdk.jshell\"`\n",
    "  \n",
    "  - 在 windows上: `java --list-modules | findstr \"jdk.jshell\"`\n",
    "  \n",
    "应当输出`jdk.jshell@`\n",
    "\n",
    "如果没有，请输入`java -verbose`检查第一行或者最后一行的java路径信息，确认java在JDK路径下，而不是JRE中。\n",
    "\n",
    "2. 不限类似jupyter的环境\n",
    "  - Jupyter\n",
    "  - JupyterLab\n",
    "  - nteract\n",
    "  \n",
    "### 安装\n",
    "\n",
    "1. 下载地址：<https://github.com/SpencerPark/IJava/releases>\n",
    "\n",
    "可选择下载压缩包：ijava-$version.zip\n",
    "\n",
    "2. 解压，看到文件install.py 和 一个java文件夹\n",
    "\n",
    "查看安装帮助：\n",
    "\n",
    "```sh\n",
    "python3 install.py -h\n",
    "```\n",
    "\n",
    "安装：\n",
    "\n",
    "```sh\n",
    "python3 install.py --sys-prefix\n",
    "```\n",
    "\n",
    "其他安装选项：`--default`, `--user`, `--sys-prefix`, `--prefix`, `--path`, or `--legacy`,不同选项对应不同安装位置。\n",
    "\n",
    "查看已经安装的内核，应当包含java，如图：\n",
    "\n",
    "```\n",
    "jupyter kernelspec list\n",
    "```\n",
    "\n",
    "![untitled.png](assets/untitled.png)\n",
    "\n",
    "### 使用\n",
    "\n",
    "导包：\n",
    "直接导入\n",
    "```\n",
    "%jars import org.slf4j.Logger;\n",
    "%jars import org.slf4j.LoggerFactory;\n",
    "```\n",
    "maven 导入\n",
    "```\n",
    "%%loadFromPOM\n",
    "<dependency>\n",
    "  <groupId>ch.qos.logback</groupId>\n",
    "  <artifactId>logback-core</artifactId>\n",
    "  <version>1.2.3</version>\n",
    "</dependency>\n",
    "```\n",
    ">import ch.qos.logback.core.Layout;\n",
    "\n",
    "或者\n",
    "```\n",
    "%maven org.slf4j:slf4j-api:1.7.25 ch.qos.logback:logback-core:1.2.3 ch.qos.logback:logback-classic:1.2.3\n",
    "\n",
    "import org.slf4j.logger;\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 支持javascript\n",
    "\n",
    "项目地址： https://github.com/n-riesco/ijavascript\n",
    "\n",
    "- 依赖：Node.js、npm\n",
    "    > sudo apt-get install nodejs npm\n",
    "\n",
    "### 安装\n",
    "\n",
    "```sh\n",
    "npm install -g ijavascript\n",
    "ijsinstall\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 支持Node JS - jupyter-nodejs\n",
    "\n",
    "项目地址：https://github.com/notablemind/jupyter-nodejs\n",
    "\n",
    "使用效果：[Example](http://nbviewer.jupyter.org/gist/jaredly/404a36306fdee6a1737a)\n",
    "\n",
    "### 安装依赖\n",
    "\n",
    "- IPython 3.x\n",
    "- pkg-config\n",
    "\n",
    "    >sudo apt install pkg-config\n",
    "    \n",
    "- 安装Node\n",
    "    >sudo apt-get install nodejs  \n",
    "    >sudo apt-get install npm\n",
    "    \n",
    "- 检查node-gyp 是否安装\n",
    "\n",
    "    >node-gyp list\n",
    "    \n",
    "    未安装的请执行：`npm install -g node-gyp`\n",
    "    \n",
    "    \n",
    "- ubuntu下安装ZeroMQ：\n",
    "\n",
    "    >apt-get install libzmq3-dev\n",
    "    \n",
    "    其他系统安装方式： https://zeromq.org/download/\n",
    "\n",
    "### 安装\n",
    "\n",
    "```sh\n",
    "git clone https://github.com/notablemind/jupyter-nodejs.git --depth=1\n",
    "cd jupyter-nodejs\n",
    "mkdir -p ~/.ipython/kernels/nodejs/\n",
    "npm install && node install.js\n",
    "npm run build\n",
    "npm run build-ext\n",
    "```\n",
    "\n",
    "### 尝试\n",
    "\n",
    "```sh\n",
    "jupyter console --kernel nodejs\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 👍其他内核支持\n",
    "\n",
    "<https://github.com/jupyter/jupyter/wiki/Jupyter-kernels>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "nbTranslate": {
   "displayLangs": [
    "*"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "en",
   "targetLang": "fr",
   "useGoogleTranslate": true
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "276.922px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
